package com.qianxun.module.datasource1.common.rest;

import com.qianxun.framework.common.util.DateUtil;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.base.bo.UserInfo;
import com.qianxun.module.base.constant.Result;
import com.qianxun.module.base.rest.BaseRestController;
import com.qianxun.module.datasource1.common.service.DataTemplateService;
import com.qianxun.module.datasource1.common.entity.DataTemplate;
import com.qianxun.module.datasource1.common.dto.DataTemplateDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* com.qianxun.module.datasource1.common.entity.DataTemplate模型http接口处理
*
* @author: admin
* @date: 2020-02-05
* @version: 1.0.0
*/
@RestController
@RequestMapping(value="/common/dataTemplate" ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class DataTemplateRestController extends BaseRestController {

    @Resource
    private DataTemplateService baseService;

    @PostMapping(value = {"/add"})
    public Result add(DataTemplateDTO query) {
        DataTemplate dataTemplate = new DataTemplate();
        ObjectUtil.switchAllFieldValue(dataTemplate, query);
        initUserInfo(dataTemplate);
        dataTemplate.setCreatedTime(DateUtil.now());
        dataTemplate.setUpdatedTime(DateUtil.now());
        baseService.add(dataTemplate);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"})
    public Result deleteById(@PathVariable(value = "id") Long id) {
        baseService.removeById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"})
    public Result updateById(@PathVariable(value = "id") Long id, DataTemplateDTO query) {
        DataTemplate dataTemplate = new DataTemplate();
        ObjectUtil.switchAllFieldValue(dataTemplate, query);
        dataTemplate.setUpdatedTime(DateUtil.now());

        baseService.updateById(dataTemplate);
        return Result.success();
    }

    @RequestMapping(value = {"/search/page"})
    public Result searchPage(@RequestParam(required = false) Integer current ,@RequestParam(required = false) Integer size
        ,@RequestParam(required = false) String sign ,@RequestParam(required = false) String title){
        UserInfo userInfo = getCurrentUserInfo();
        IPage<DataTemplate> page =  baseService.getPage(userInfo.getUserKey() ,sign ,title ,current ,size);
        return Result.success(page);
    }

    @RequestMapping(value = {"/search/id/{id}"})
    public Result searchOneById(@PathVariable(value = "id") Long id) {
        DataTemplate DataTemplate = baseService.getById(id);
        return Result.success(DataTemplate);
    }
}
